<?php

class PointRecordController extends Controller {
    
       /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout = '//layouts/column2';

    /**
     * @return array action filters
     */
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
            'postOnly + delete', // we only allow deletion via POST request
        );
    }

    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('index', 'view'),
                'users' => array('@'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' and 'admin' actions
                'actions' => array('create', 'update'),
                'users' => array('@'),
            ),
            array('allow', // allow authenticated user to perform 'admin' and 'delete' actions
                'actions' => array('admin', 'delete'),
                'users' => array('@'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function actionAdmin() {
        $model = new PointRecord('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['PointRecord']))
            $model->attributes = $_GET['PointRecord'];
        
        $this->render('admin', array(
            'model' => $model,
        ));
    }

    public function actionCreate() {
        
       $model = new PointRecord;
       if (isset($_POST['PointRecord'])) {
           $cca_name = $_POST['PointRecord']['cca_id'];
            $criteria = new CDbCriteria;
            $criteria->condition = "name = '" . $cca_name . "'";
            $cca = Cca::model()->find($criteria);
            $matric_nos2 = explode('(', $_POST['PointRecord']['matric_no']);
            $matric_no2 = trim($matric_nos2[1], ')');
            $criteria = new CDbCriteria;
            $criteria->condition = "matric_no = '" . $matric_no2 . "'";
            $resident = Resident::model()->find($criteria);
            $_POST['PointRecord']['matric_no'] = $resident->getAttribute('matric_no');

            $_POST['PointRecord']['cca_id'] = $cca->getAttribute('id');
            $model->attributes = $_POST['PointRecord'];
            $validCreate = true;
           
            //Validation whether currentUser is allowed to add points to members of that committee
            if (isset(Yii::app()->user->userModel)) {
                $validCreate = false;
                $cca_residents = Yii::app()->user->userModel->ccaResidents;
                foreach (Yii::app()->user->userModel->ccaResidents as $ccaResident) {
                    if ($ccaResident->cca_id == $model->cca_id && $ccaResident->role_level <= 5) {//if cca matches						
                        $validCreate = true;
                    }
                }
            }
            
            
            if ($validCreate && $model->save())
                $this->redirect(array('admin'));
            else
                $this->redirect(array('site/index'));
        }
        $this->render('create', array(
            'model' => $model,
        ));
    }
    
    
    
    public function actionDelete($id) {
        $model = PointRecord::model()->findByPk($id);
        $validDelete = true;
        //Validation whether currentUser is allowed to remove residents from committees
        if (isset(Yii::app()->user->userModel)) {
            $validDelete = false;
            $cca_residents = Yii::app()->user->userModel->ccaResidents;
            foreach (Yii::app()->user->userModel->ccaResidents as $ccaResident) {
                if ($ccaResident->cca_id == $model->cca_id && $ccaResident->role_level <= 3 && $ccaResident->matric_no != $model->matric_no) {//if cca matches						
                    $validDelete = true;
                }
            }
        }
        if ($validDelete){
            $model->delete();
        } else
            $this->redirect(array('site/index'));
        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if (!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
    }
    
    

    public function actionIndex() {
        $this->render('index');
    }

    public function actionUpdate() {
        $this->render('update');
    }

    public function actionView() {
        $this->render('view');
    }

    // Uncomment the following methods and override them if needed
    /*
      public function filters()
      {
      // return the filter configuration for this controller, e.g.:
      return array(
      'inlineFilterName',
      array(
      'class'=>'path.to.FilterClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }

      public function actions()
      {
      // return external action classes, e.g.:
      return array(
      'action1'=>'path.to.ActionClass',
      'action2'=>array(
      'class'=>'path.to.AnotherActionClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }
     */
    
        protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'point-record-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}
